package com.jflm.service.impl;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.stereotype.Service;

import com.jflm.service.IPropertyService;

/**
 * ******************  类说明  *********************
 * class       :  PropertyServiceImpl
 * @author     :  hejinyun@umpay.com
 * @version    :  1.0  
 * description :  配置文件读取服务类接口实现
 * @see        :                        
 * ***********************************************
 */
@Service("propertyService")
public class PropertyServiceImpl implements IPropertyService {
	
	private static final Logger LOG = LoggerFactory.getLogger(PropertyServiceImpl.class);
	@Autowired
	private MessageSource messageSource;
	
	@Override
	public String getStrValue(String key, String defaultValue) {
		String value = localPropertyValue(messageSource, key, defaultValue);
		return value;
	}

	@Override
	public int getIntValue(String key, int defaultValue) {
		String value = localPropertyValue(messageSource, key, ""+defaultValue);
		return (null == value)?defaultValue:Integer.parseInt(value);
	}
	
	protected String localPropertyValue(MessageSource messageSource, String key, String defaultValue){
		String value = null;
		try {
			value = StringUtils.trimToNull(messageSource.getMessage(key, null, defaultValue, Locale.CHINA));
		} catch (NoSuchMessageException e){
			LOG.error("#localPropertyValue()", e);
		}
		LOG.info("#localPropertyValue() Key::{}=[{}]", key, value);
		return value;
	}
}
